package com.ogawa.project8506e.activity

import android.content.Intent
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.ogawa.base.base.BaseActivity
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.project8506e.R
import kotlinx.android.synthetic.main.activity_8506e_ai_recommend.*
import kotlin.random.Random

@Route(path = UrlPathConstant.ACTIVITY_8506E_AI_RECOMMEND)
class Device8506eAiRecommendActivity : BaseActivity(), View.OnClickListener {
    private val ageProgram = arrayOf(9, 1, 10)
    private val shoesProgram = arrayOf(3, 6, 7, 9)
    private val sportsProgram = arrayOf(7, 9, 1, 6)
    private val todayProgram = arrayOf(4, 4, 10, 6)
    private val sleepProgram = arrayOf(1, 6, 4, 10)
    private val finalProgram = IntArray(5)

    override fun getLayoutId(): Int {
        return R.layout.activity_8506e_ai_recommend
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.ai_recommend))
        tv_fenghua.setOnClickListener(this)
        tv_yiqi.setOnClickListener(this)
        tv_laodang.setOnClickListener(this)
        tv_gaogen.setOnClickListener(this)
        tv_pixie.setOnClickListener(this)
        tv_yundongxie.setOnClickListener(this)
        tv_tuoxie.setOnClickListener(this)
        tv_liebao.setOnClickListener(this)
        tv_tuzi.setOnClickListener(this)
        tv_luotuo.setOnClickListener(this)
        tv_kaola.setOnClickListener(this)
        tv_shuaju.setOnClickListener(this)
        tv_youxi.setOnClickListener(this)
        tv_jiawu.setOnClickListener(this)
        tv_yuedu.setOnClickListener(this)
        tv_gaozheng.setOnClickListener(this)
        tv_jiaban.setOnClickListener(this)
        tv_yule.setOnClickListener(this)
        tv_shimian.setOnClickListener(this)
        tv_confirm.setOnClickListener(this)
        tv_cancel.setOnClickListener(this)
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.tv_fenghua -> {
                setAge(0)
            }
            R.id.tv_yiqi -> {
                setAge(1)
            }
            R.id.tv_laodang -> {
                setAge(2)
            }
            R.id.tv_gaogen -> {
                setShoes(0)

            }
            R.id.tv_pixie -> {
                setShoes(1)
            }
            R.id.tv_yundongxie -> {
                setShoes(2)
            }
            R.id.tv_tuoxie -> {
                setShoes(3)
            }
            R.id.tv_liebao -> {
                setSports(0)
            }
            R.id.tv_tuzi -> {
                setSports(1)
            }
            R.id.tv_luotuo -> {
                setSports(2)
            }
            R.id.tv_kaola -> {
                setSports(3)
            }
            R.id.tv_shuaju -> {
                setToday(0)
            }
            R.id.tv_youxi -> {
                setToday(1)
            }
            R.id.tv_jiawu -> {
                setToday(2)
            }
            R.id.tv_yuedu -> {
                setToday(3)
            }
            R.id.tv_gaozheng -> {
                setSleep(0)
            }
            R.id.tv_jiaban -> {
                setSleep(1)
            }
            R.id.tv_yule -> {
                setSleep(2)
            }
            R.id.tv_shimian -> {
                setSleep(3)
            }
            R.id.tv_cancel -> finish()
            R.id.tv_confirm -> pickAiProgram()
        }
    }

    private fun setAge(i: Int) {
        finalProgram[0] = ageProgram[i]
        tv_fenghua.isSelected = i == 0
        tv_yiqi.isSelected = i == 1
        tv_laodang.isSelected = i == 2
    }

    private fun setShoes(i: Int) {
        finalProgram[1] = shoesProgram[i]
        tv_gaogen.isSelected = i == 0
        tv_pixie.isSelected = i == 1
        tv_yundongxie.isSelected = i == 2
        tv_tuoxie.isSelected = i == 3
    }


    private fun setSports(i: Int) {
        finalProgram[2] = shoesProgram[i]
        tv_liebao.isSelected = i == 0
        tv_tuzi.isSelected = i == 1
        tv_luotuo.isSelected = i == 2
        tv_kaola.isSelected = i == 3
    }

    private fun setToday(i: Int) {
        finalProgram[3] = shoesProgram[i]
        tv_shuaju.isSelected = i == 0
        tv_youxi.isSelected = i == 1
        tv_jiawu.isSelected = i == 2
        tv_yuedu.isSelected = i == 3
    }


    private fun setSleep(i: Int) {
        finalProgram[4] = shoesProgram[i]
        tv_gaozheng.isSelected = i == 0
        tv_jiaban.isSelected = i == 1
        tv_yule.isSelected = i == 2
        tv_shimian.isSelected = i == 3
    }


    private fun pickAiProgram() {
        if (finalProgram.contains(0)) {
            Log.e("tag", "有选项未选")
            return
        } else {
            val nextInt = Random.nextInt(0, 5)
            val program = finalProgram[nextInt].toString()
            setResult(RESULT_OK, Intent().putExtra("program", program))
            finish()
        }
    }
}